<template>
  <div class="container">

  </div>
</template>

<script>
import { getQueryString } from '../utils/common';
import { mapState } from 'vuex';
import config from '../utils/config';

export default {
    name: 'login',
    computed: {
        ...mapState(['userNum'])
    },
    data () {
        return {

        };
    },
    mounted () {
        this.init();
    },
    methods: {
        init () {
            let code = this.$route.query.code || getQueryString('code');
            let redirect = this.$route.query.redirect || getQueryString('redirect');
            if (redirect) sessionStorage.setItem('redirect', redirect);
            if (code) {
                this.getUserNum(code);
            } else {
                location.replace(config.wx.link);
            }
        },
        getUserNum (code) {
            console.log('用code获取微信个人信息');
            this.$http.get(`/vank/receipt/user_info1.do?code=${code}`).then(res => {
                let data = JSON.parse(res.data.data);
                if (+data.errcode !== 0) {
                    console.log('微信个人信息获取失败');
                    if (+data.errcode === 40029) {
                        location.replace(config.wx.link);
                    } else {
                        console.log(data.errmsg);
                    }
                } else {
                    if (data.userid) {
                        console.log('微信个人信息获取成功>>>', data);
                        let { userid, avatar } = data;
                        if (userid === 'v-meisc') {
                            userid = 'zhaozm01';
                            alert('梅小西的账号，切换成志敏的数据');
                        }
                        this.$store.commit('userNum', userid);
                        this.$store.commit('avatar', avatar);
                        this.getUserInfo();
                    } else {
                        console.log('没有关注');
                    }
                }
            });
        },
        loginEAS () {
            // dcName Eastest（正式环境换成eas750）
            var url = this.HOST + '?method=loginEAS&userName=sysWS&password=sysWS&slnName=eas&dcName=eas750&language=L2&dbType=1';
            this.$http.get(url).then(res => {
                var sessionId = res.data.msg;
                this.$store.commit('sessionId', sessionId);
            });
        },
        getUserInfo () {
            console.log('获取费控用户信息');
            var url = this.HOST + '?method=getAccountBank&userNum=' + this.userNum;
            this.$http.get(url).then(res => {
                if (res.data) {
                    let userInfo = res.data;
                    console.log('getAccountBank', userInfo);
                    this.$store.commit('userInfo', userInfo);
                    let redirect = sessionStorage.getItem('redirect') || '/';
                    this.$router.replace(redirect);
                }
            });
        }
    }
};
</script>

<style lang="scss" scoped>
</style>
